﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.IO;

namespace TDBaseDataLibrary
{
    class EnemyClass : RoleClass
    {
        int goldlevel;
        int treaturelevel;
        EnemySprite sprite;
        public static GameSprite Explosion;
        public int GoldLevel
        {
            get { return goldlevel; }
            set { goldlevel = value; }
        }
        public int TreatureLevel
        {
            get { return treaturelevel; }
            set { treaturelevel = value; }
        }
    }
    

    public class Enemy : TDCharacter
    {
        static RoleClasss EnemyType;
        static List<RoleSprite> RoleList;
//      levelconfig m_levelEnv;
        List<int> m_Path = new List<int>();
        float m_fPathLength;
        int m_nCurPathIndex;
        //附加特技属性

        public TDBaseDataLibrary.RoleClass eclass;
        public RoleSprite Sprite;

        //附加属性
        public int id;
        public int index;
        public float t;
        public float time;
        public float speed;
        //public Vector2 pos;
        //public int m_nPos;
        //public int m_nEnd;
        //public levelpath path;
        public int nimgindex;
        public float lift;
        public int maxlift;
        public static GameSprite Explosion;
        

        EnemySprite sprite;
        //附属属性
        Attribute_Effect[] effect;
        bool beginDead;
        bool disappear;
        static Texture2D tempTex;
        public static void LoadContent(ContentManager content)
        {
            EnemyType = content.Load<RoleClasss>("Enemy_Config");
            RoleList = new List<RoleSprite>();
            tempTex = content.Load<Texture2D>("grass");
            for (int i = 0; i < EnemyType.EnemyList.Count; i++)
            {
                Vector2 vExCenter = new Vector2(40, 40);
                RoleSprite sprite = new RoleSprite(content, EnemyType.EnemyList[i].tex, 200, true, 64, 64, 13, vExCenter);
                RoleList.Add(sprite);
            }
        }
        public static void LoadEnemyClass(ContentManager content, string szfile)
        {
            //EnemyType = RoleClass.LoadRoleClass(content, szfile);
        }
        public bool HitTest(int X,int Y)
        {
//            Rectangle rt = new Rectangle((int)(m_pos.X - Sprite.GetCenter().X), (int)(m_pos.Y - Sprite.GetCenter().Y), Sprite.FrameWidth, Sprite.FrameHeight);
//            if (X < rt.X + rt.Width && X > rt.X && Y < rt.Y + rt.Height && Y > rt.Y)
 //           {
//                return true;
//            }
            return false;
        }
        //搜索路径，让精灵自己搜索路径
        public bool FindPath()
        {
            return true;
        }
        public void Decision()
        {
            
        }
        public Enemy(string szid, levelpath lpath, float fstarttime)
        {
            //int nCount = EnemyType.EnemyList.Count;
            //for (int i = 0; i < nCount; i++)
            //{
            //    if (EnemyType.EnemyList[i].caption == szid)
            //    {
            //        eclass = EnemyType.EnemyList[i];
            //        Sprite = RoleList[i];
            //        break;
            //    }
            //}
            //t = 0;
            //state = spritestate.liveing;
            //disappear = false;
            //path = lpath;
            //m_pos = new Vector2(100, 100);
            //speed = eclass.get_attribute(enumattr.attr_speed);
            //time = fstarttime;
            //lift = eclass.get_attribute(enumattr.attr_lift);

        }

        public Vector2 Pos
        {
            get { return m_pos; }
            set { m_pos = value; }
        }
        public float Lift
        {
            get { return lift; }
            set { lift = value; }
        }
        public bool Disappear
        {
            set{ 
                if(value == true) 
                    state = spritestate.disappear;
                else
                    state = spritestate.liveing;
            }
            get
            {
                return (state == spritestate.disappear);
            }
        }
        public bool Dead
        {
            get { return (state == spritestate.deading); }
            set {
                if (value == true)
                {
                    time = 0;
                    nimgindex = 0;
                    state = spritestate.deading;
                }
                else
                    state = spritestate.liveing;
            
            }
        }
        public override bool Update(GameTime gametime)
        {
            if (base.Update(gametime) == true)
            {
                if (state == spritestate.deading)
                {
                    time += (float)gametime.ElapsedGameTime.TotalMilliseconds;
                    if (time > 40)
                    {
                        time -= 40;
                        nimgindex++;
                        if (nimgindex >= 20)
                            state = spritestate.disappear;
                    }
                }
                else if (state == spritestate.liveing)
                {
                    int ntime = (int)(gametime.TotalGameTime.TotalMilliseconds - time);
                    int nindex = ntime / 200;
                    nimgindex = nindex % 2 + 11 + m_direct * 13;

                }
            }
            
            return true;
        }

        public override void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch,int OffsetX,int OffsetY)
        {
            Vector2 v = new Vector2(m_pos.X + OffsetX, m_pos.Y + OffsetY);
            //int nPos = m_Path[m_nCurPathIndex];
            //int Y = nPos/m_levelEnv.m_Map.m_nCellWidth;
            //int X = nPos%m_levelEnv.m_Map.m_nCellHeight;


            //Vector2 v = new Vector2(m_levelEnv.m_Map.m_cellPixel * X + OffsetX, m_levelEnv.m_Map.m_cellPixel * Y + OffsetY);
            //int count = m_Path.Count;
            //for(int i = 0;i < count;i ++)
            //{
            //    int pos = m_Path[i];
            //    int x = pos % m_levelEnv.m_Map.m_nCellWidth;
            //    int y = pos / m_levelEnv.m_Map.m_nCellWidth;
            //    Rectangle rtDes = new Rectangle(x * 8, y * 8, 8, 8);
            //    rtDes.Offset(OffsetX, OffsetY);
            //    spriteBatch.Draw(tempTex, rtDes, Color.White);
            //    //Sprite.Draw(graphics, gameTime, spriteBatch, nimgindex, v, 0.0f, (int)lift * 100 / eclass.get_attribute(enumattr.attr_lift), Color.White);
            //}
           
            if (state == spritestate.liveing)
            {
                int nCount = GetGeneCount();
                for (int i = 0; i < nCount; i++)
                {
                    TDGene g = GetGene(i);
                    g.gene_pic.actionsprite[0].Draw(graphics, gameTime, spriteBatch, nimgindex, v, 0.0f, Color.White);
                }
                //Sprite.Draw(graphics, gameTime, spriteBatch, nimgindex, v, 0.0f, (int)(lift * 100 / GetStatus(enumstatus.status_lift)), Color.White);
                //sprite.Draw(graphics, gameTime, spriteBatch, nimgindex, v, 0.0f, (int)lift * 100 / maxlift);
            }
            else if (state == spritestate.deading)
            {
                Explosion.Draw(graphics, gameTime, spriteBatch, nimgindex, v, 0.0f, Color.White);
            }
        }
    }
}
